package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.CommonUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.ZsyyCommunity;
import com.ruoyi.system.mapper.ZsyyCommunityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyBannerAddBo;
import com.ruoyi.system.bo.ZsyyBannerQueryBo;
import com.ruoyi.system.bo.ZsyyBannerEditBo;
import com.ruoyi.system.domain.ZsyyBanner;
import com.ruoyi.system.mapper.ZsyyBannerMapper;
import com.ruoyi.system.vo.ZsyyBannerVo;
import com.ruoyi.system.service.IZsyyBannerService;
import com.ruoyi.system.mapper.ZsyyBannerMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 资讯轮播图Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyBannerServiceImpl extends ServicePlusImpl<ZsyyBannerMapper, ZsyyBanner> implements IZsyyBannerService {
    @Autowired
    private ZsyyBannerMapper iZsyyBannerMapper;
	@Autowired
	private ZsyyCommunityMapper communityMapper;

    @Override
    public ZsyyBannerVo queryById(Long id){
        return getVoById(id, ZsyyBannerVo.class);
    }

    @Override
    public TableDataInfo<ZsyyBannerVo> queryPageList(ZsyyBannerQueryBo bo) {
        PagePlus<ZsyyBanner, ZsyyBannerVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyBannerVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyBannerVo> queryList(ZsyyBannerQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyBannerVo.class);
    }

    private LambdaQueryWrapper<ZsyyBanner> buildQueryWrapper(ZsyyBannerQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyBanner> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommunityId() != null, ZsyyBanner::getCommunityId, bo.getCommunityId());
        lqw.eq(StrUtil.isNotBlank(bo.getTitle()), ZsyyBanner::getTitle, bo.getTitle());
        lqw.eq(StrUtil.isNotBlank(bo.getBannerUrl()), ZsyyBanner::getBannerUrl, bo.getBannerUrl());
        lqw.eq(bo.getType() != null, ZsyyBanner::getType, bo.getType());
        lqw.eq(bo.getStatus() != null, ZsyyBanner::getStatus, bo.getStatus());
        lqw.eq(bo.getAboutId() != null, ZsyyBanner::getAboutId, bo.getAboutId());
        lqw.like(StrUtil.isNotBlank(bo.getCommunityName()), ZsyyBanner::getCommunityName, bo.getCommunityName());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyBannerAddBo bo) {
		if (!CommonUtil.isEmpty(bo.getCommunityId()) && bo.getCommunityId() != 0){
			ZsyyCommunity community = communityMapper.selectById(bo.getCommunityId());
			bo.setCommunityName(community.getName());
		}else{
			bo.setCommunityName("公共内容");
		}
        ZsyyBanner add = BeanUtil.toBean(bo, ZsyyBanner.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyBannerEditBo bo) {
		if (!CommonUtil.isEmpty(bo.getCommunityId()) && bo.getCommunityId() != 0){
			ZsyyCommunity community = communityMapper.selectById(bo.getCommunityId());
			bo.setCommunityName(community.getName());
		}else{
			bo.setCommunityName("公共内容");
		}
        ZsyyBanner update = BeanUtil.toBean(bo, ZsyyBanner.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyBanner entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
